

package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.ZtSet;
import com.tz.tzgw.pc.web.model.ZtVideoCate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ZtVideoCateDao {
    private final JdbcTemplate jdbcTemplate;

    public ZtVideoCateDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<ZtVideoCate> getList(Long setid) {
        String sql = "SELECT * FROM tz_zt_video_cate WHERE setid = ? ORDER BY sort_order DESC";
        List<ZtVideoCate> catelist = jdbcTemplate.query(sql,new Object[]{setid}, new BeanPropertyRowMapper<>(ZtVideoCate.class));

        return catelist != null ? catelist : new ArrayList<ZtVideoCate>();
    }

    public int insertOrUpdateCate(ZtVideoCate ztVideoCate) {
        if (ztVideoCate.getId() != null) {
            String sql = "UPDATE tz_zt_video_cate SET name=?, sort_order=? WHERE id=?";
            jdbcTemplate.update(sql, ztVideoCate.getName(), ztVideoCate.getSort_order(),ztVideoCate.getId());
        } else {
            String sql = "INSERT INTO tz_zt_video_cate (name,setid, sort_order) VALUES (?, ?, ?)";
            jdbcTemplate.update(sql, ztVideoCate.getName(),ztVideoCate.getSetid(), ztVideoCate.getSort_order());
        }
        return  1;
    }
    public Integer deleteCate(Integer id) {
        String sql = "DELETE FROM tz_zt_video_cate WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }

}

